
// Call it through the Events in the form:
// ["d_c_e_reinf",[[12,34,56],200,"PARA",1]] call XNetCallEvent

if (!isServer) exitWith {};
private ["_target_radius","_reinforcement_type","_enemy_power","_attack_pos","_retry","_end_pos","_i"];

["reinforcement_canceled",False] call XNetSetJIP;

if (isNil "_this") exitWith {diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","No parameters"];};
if (typename _this != "ARRAY") exitWith {diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","Parameter is not an array"];};
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"sendreinforcement.sqf","Input",_this];

_target_pos = _this select 0;
if (count _this > 1) then {_target_radius = _this select 1;};
if (count _this > 2) then {_reinforcement_type = _this select 2;};
if (count _this > 3) then {_enemy_power = _this select 3;};
diag_log Format["(%1)[%2]: %3: %4, %5, %6, %7",diag_tickTime,"sendreinforcement.sqf","Start Parameters",_target_pos, _target_radius, _reinforcement_type, _enemy_power];

if (isNil "_target_pos") exitWith {diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","Not a valid position"];};
if (typename _target_pos != "ARRAY") exitWith {diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","Not a valid position"];};
if (count _target_pos < 3) exitWith {diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","Not a valid position"];};
if (isNil "_target_radius") then { _target_radius = 200 };
if (_target_radius < 50) then {_target_radius = 50 };
if (isNil "_reinforcement_type") then { _reinforcement_type = "PARA" };
if (isNil "_enemy_power") then { _enemy_power = 1 };
if (_enemy_power <= 0) then { _enemy_power = 1 };
diag_log Format["(%1)[%2]: %3: %4, %5, %6, %7",diag_tickTime,"sendreinforcement.sqf","Start Parameters",_target_pos, _target_radius, _reinforcement_type, _enemy_power];
_attack_pos = [_target_pos,(_target_radius / 2)] call XfGetRanPointCircleOld;
_retry = 10;
while {((_retry > 0) And (count _attack_pos < 3))} do {
	_attack_pos = [_target_pos,(_target_radius / 2)] call XfGetRanPointCircleOld;
	_retry = _retry - 1;
};
If ((count _attack_pos < 3)) exitWith {diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"sendreinforcement.sqf","Could not generate an attack pos",_attack_pos];};

_can_send_reinforcement = true;
// If we reached this point, all values are valid. Now we check if a reinforcement is already on the way to this position or the distance is less than radius.
If (isNil "d_current_reinforcement_groups") then {d_current_reinforcement_groups = []};
If (count d_current_reinforcement_groups > 0) then {
	{
		private ["_curr_reinf_pos","_distance"];
		_curr_reinf_pos = _x select 0;
		_distance = _target_pos distance _curr_reinf_pos;
		if (_distance <= _target_radius) exitWith {
				diag_log Format["(%1)[%2]: %3 (%4)",diag_tickTime,"sendreinforcement.sqf","There is already a reinforcement going there!",_distance];
				_can_send_reinforcement = false;
			};
	} foreach d_current_reinforcement_groups;
};

If (_can_send_reinforcement) then {
	d_current_reinforcement_groups set [count d_current_reinforcement_groups, [_target_pos,_target_radius]];
	diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"sendreinforcement.sqf","Sending Reinforcement at",_attack_pos];
	
	// TODO: Add ROAD reinforcement
	//		 Add FEET reinforcement
	//		 Add WATER reinforcement
	switch (_reinforcement_type) do {
		default {
			_start_pos = call XfGetRanPointOuterAir;
			while {(_start_pos distance FLAG_BASE) < 1000} do {
				_start_pos = call XfGetRanPointOuterAir;
			};
			_end_pos = call XfGetRanPointOuterAir;
			while { (_end_pos distance FLAG_BASE) < 1000} do {
				_end_pos = call XfGetRanPointOuterAir;
			};
			diag_log Format["(%1)[%2]: %3 (%4)",diag_tickTime,"sendreinforcement.sqf","Calling the default function",isnil "x_createpara3x"];
			[_start_pos,_attack_pos,_target_radius,_end_pos,_enemy_power] call x_createpara3x;
			for [{_i = (count d_current_reinforcement_groups - 1)},{_i >= 0},{_i = _i - 1}] do {
				if (Format["%1",((d_current_reinforcement_groups select _i) select 0)] == Format ["%1",_target_pos]) then {
					diag_log Format["(%1)[%2]: %3 (%4)",diag_tickTime,"sendreinforcement.sqf","Erased target position",_i];
					d_current_reinforcement_groups set [_i, "XdelX"];
					d_current_reinforcement_groups = d_current_reinforcement_groups - ["XdelX"];
				};
			};
		};
	};
	diag_log Format["(%1)[%2]: %3",diag_tickTime,"sendreinforcement.sqf","The reinforcement call has been completed"];
};
